package org.wlw.teacher.utils;

import org.wlw.teacher.context.SystemContext;
import org.wlw.teacher.models.BaseModel;
import org.wlw.teacher.models.Pager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WebUtils {
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String dateToString(Date date) {
        return sdf.format(date);
    }

    public static Date strToDate(String sdate) {
        try {
            return sdf.parse(sdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setPagerObject(BaseModel bm) {
        int start = (SystemContext.getPageIndex()-1)*SystemContext.getPageSize();
        bm.setPageSize(SystemContext.getPageSize());
        bm.setPageStart(start);
    }

    public static void setPager(Pager<? extends BaseModel> pager,int totals) {
        pager.setPageIndex(SystemContext.getPageIndex());
        pager.setTotals(totals);
        pager.setPageSize(SystemContext.getPageSize());
        pager.setPageTotal(((totals-1)/SystemContext.getPageSize())+1);
    }
}
